import { defineStore } from 'pinia'
import apis from '@/plugins/http'
import tokenUtil from '@/utils/token'


export default defineStore('useUserStore', {
    state: () => reactive({
        tokenName: tokenUtil.getTokenName() || '',
        token: tokenUtil.getToken() || '',
        refreshToken: tokenUtil.getRefreshToken() || '',
        roles: [],
        permissions: [],
        userInfo: {
            id: undefined,
            avatar: undefined,
            nickName: undefined,
            userName: undefined,
            userType: undefined,
            status: undefined,
            updateTime: undefined,
            createTime: undefined,
        },
        scopeId: ''
    }),
    actions: {
        // 登录
        login(userInfo) {
            return apis.adminLogin({
                params: userInfo,
                needToken: false
            }).then(res => {
                this.setTokenName(res.tokenName)
                this.setToken(res.token)
                this.setRefreshToken(res.refreshToken)
                return Promise.resolve('登录成功')
            }).catch(err => {
                return Promise.reject(err)
            })
        },

        setToken(token) {
            if (token) {
                this.token = token
                tokenUtil.setToken(token)
            } else {
                this.token = null
                tokenUtil.removeToken()
            }
        },
        setTokenName(tokenName) {
            if (tokenName) {
                this.tokenName = tokenName
                tokenUtil.setTokenName(tokenName)
            } else {
                this.tokenName = null
                tokenUtil.removeTokenName()
            }
        },
        setRefreshToken(refreshToken) {
            if (refreshToken) {
                this.refreshToken = refreshToken
                tokenUtil.setRefreshToken(refreshToken)
            } else {
                this.refreshToken = null
                tokenUtil.removeRefreshToken()
            }
        },
        setRoles(roles) {
            this.roles = roles
        },
        setPermissions(permissions) {
            this.permissions = permissions
        },
        setScopeId(scopeId) {
            this.scopeId = scopeId
        },
        setUserInfo(userInfo) {
            this.userInfo = userInfo
            if (this.userInfo) {
                this.userInfo.avatar = this.userInfo.avatar || new URL('@/assets/images/photo.jpeg', import.meta.url).href
            }
        },
        // 获取用户信息
        getInfo() {
            return apis.getAdminInfo().then(res => {
                this.setUserInfo(res.user)
                if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                    this.setRoles(res.roles)
                    this.setPermissions(res.permissions)
                    this.setScopeId(res.user.scopeId)
                } else {
                    this.setRoles([])
                }
                return Promise.resolve('登录成功')
            }).catch(err => {
                return Promise.reject(err)
            })
        },

        // 退出系统
        logOut() {
            return apis.logout({
                loading: true,
                loadingText: '退出系统中，请稍后...'
            }).then(res => {
                this.clearLoginInfo()
                return Promise.resolve()
            }).catch(err => {
                this.clearLoginInfo()
                return Promise.reject(err)
            })
        },

        // 前端 登出
        fedLogOut() {
            return new Promise(resolve => {
                this.clearLoginInfo()
                resolve()
            })
        },
        clearLoginInfo() {
            this.setTokenName('')
            this.setToken('')
            this.setRefreshToken('')
            this.setRoles([])
            this.setPermissions([])
            this.setUserInfo(null)
        },
        getToken() {
            if (!this.token) {
                this.token = tokenUtil.getToken()
            }
            return this.token
        },
        getRefreshToken() {
            if (!this.refreshToken) {
                this.refreshToken = tokenUtil.getRefreshToken()
            }
            return this.refreshToken
        }
    }
})
